package com.kaspersky.gcmconnectivitytest;

import java.io.File;
import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.util.Patterns;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends Activity {

    private TextView mIdentityInput;
    
    private SharedPreferences mSettings;
    
    private static boolean mIsStarted = false;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(mIsStarted)
            findViewById(R.id.button1).setVisibility(View.GONE);

        mIdentityInput = (TextView)findViewById(R.id.identity_input);
        mSettings      = getSharedPreferences("fuck.dat", 0);

        final String identity = mSettings.getString("user_identity", null);
        
        if(identity == null)
        {
            final Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
            final Account[] accounts = AccountManager.get(this).getAccounts();
            for (Account account : accounts) {
                if (emailPattern.matcher(account.name).matches()) {
                    mIdentityInput.setText(account.name);
                    break;
                }
            }
        }
        else
            mIdentityInput.setText(identity);

        // Project: 44189536843
        // Server API Key: AIzaSyB9evM2EZNT7yA4s8nxGFObMd7kOlBFkgk
            
        mIsStarted = true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void onClickDonate(View v)
    {
        new AlertDialog.Builder(this)
            .setMessage(Html.fromHtml("Подайте на Колбасоff архитекторам.<br /><br />WM кошелек: R325618944235"))
            .setPositiveButton("OK", null)
            .create()
            .show();
    }
    
    public void onClickGo(View button)
    {
        final String identity = mIdentityInput.getText().toString().trim(); 
        if(identity.length() == 0)
        {
            new AlertDialog.Builder(this)
                .setMessage("Пожалуйста, представься")
                .setPositiveButton("OK", null)
                .create()
                .show();
        }
        else
        {
            button.setVisibility(View.GONE);
            mSettings.edit().putString("user_identity", identity).commit();
            
            GCMRegistrar.checkDevice(this);
            GCMRegistrar.checkManifest(this);
            GCMRegistrar.register(this, "44189536843");
        }
    }
    
    public void onClickShare(View v)
    {
        try
        {
            final ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
            final Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("application/vnd.android.package-archive");
            
            final Uri uri = Uri.fromFile(new File(ai.sourceDir));
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(intent);
        }
        catch (Throwable e)
        {
            Toast.makeText(this, "File didn't send: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.e("GCMConnectivityTest", e.getMessage(), e);
        }
    }
}
